/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Apriori;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author Yilin/YichaoSun
 */

public class outputRules {
    
    HashMap<ArrayList<String>, Integer> largeItemSet = new HashMap<ArrayList<String>, Integer>();
    HashMap<AssoRule, Double> resultSets = new HashMap<AssoRule, Double>();
    ArrayList<HashSet<String>> transactionList = new ArrayList<HashSet<String>>();
    double minSupport;
    double minConf;
    
    public outputRules(HashMap<AssoRule, Double> resultSets, ArrayList<HashSet<String>> transactionList, HashMap<ArrayList<String>, Integer> largeItemSet, double minSupport, double minConf){
        this.resultSets = resultSets;
        this.transactionList = transactionList;
        this.largeItemSet = largeItemSet;
        this.minSupport = minSupport;
        this.minConf = minConf;
    }
    
    public void PrintResults() throws IOException{
        
        FileWriter fw = new FileWriter("output.txt");
        BufferedWriter bf = new BufferedWriter(fw);
        
        //Sort the large item set
	ArrayList largeItemSetEntry= new ArrayList(this.largeItemSet.entrySet());
	Collections.sort(largeItemSetEntry, new MyComparator.CompareInt());
        //Print large Item Set
        bf.write("==Large itemsets (min_sup="+minSupport*100+"%)\n");
        Iterator<Map.Entry<ArrayList<String>,Integer>> largeItemSetIter = largeItemSetEntry.iterator();
        while(largeItemSetIter.hasNext()){
            Map.Entry<ArrayList<String>, Integer> curItemSet = largeItemSetIter.next();
            float tmpValue = (float)((curItemSet.getValue() * 100)/ transactionList.size());
            bf.write("["+curItemSet.getKey().toString()+"], "+ tmpValue + "%\n" );
        }
        bf.write("\n");
        
        //Print Rules
        ArrayList tempRuleSet = new ArrayList(resultSets.entrySet());
	Collections.sort(tempRuleSet, new MyComparator.CompareDouble());
        bf.write("==High-confidence association rules (min_conf=" + minConf*100 + "%)\n");
        Iterator<Map.Entry<AssoRule, Double>> ruleIter = tempRuleSet.iterator();
        while(ruleIter.hasNext()){
            Map.Entry<AssoRule, Double> curRule = ruleIter.next();
            ArrayList<String> tempR = new ArrayList<String>();
            String LHS = curRule.getKey().getLHS().toString();
            String RHS = curRule.getKey().getRHS().toString();
            tempR.add(LHS);
            tempR.add(RHS);
            float curSup = (float) (100.0 * largeItemSet.get(tempR)/transactionList.size());
            double curConf = curRule.getValue() * 100;
            bf.write("["+ LHS +"]=>"+ RHS + "(Conf:"+ curConf + ", Supp" + curSup +"%)\n");   
        }
        
        
        bf.close();
        
    }
    
    
    
    
}
